
# -*- coding: utf-8 -*-

import urllib.request
import requests
from bs4 import BeautifulSoup

class firstSearch(object):
    def __init__(self, *args, **kwargs):
        return super().__init__(*args, **kwargs)

    def __searchItem(self,itemName,itemDict,used):
        wiki="https://ff14.huijiwiki.com/"
        target="https://ff14.huijiwiki.com/wiki/%E7%89%A9%E5%93%81:"+itemName
        req=requests.get(url=target)
        html=req.text
        bf=BeautifulSoup(html)
        usedList=['-------- 用于制作 --------']
        used_text=bf.find_all('table',class_='wikitable item-recipe-table')
        if used_text!=[]:
            for each2 in used_text:
                used_text2=BeautifulSoup(str(each2))
                used_text2_list=used_text2.find_all('div',class_='item-name common') 
                for each in used_text2_list:
                    temp=BeautifulSoup(str(each))
                    usedList.append(temp.a.string)
                used_text2_list=used_text2.find_all('div',class_='item-name uncommon') 
                for each in used_text2_list:
                    temp=BeautifulSoup(str(each))
                    usedList.append(temp.a.string)
        itemDict['产量']=[str('0'),usedList]
        texts=bf.find_all('table',class_='wikitable item-craft-table filter-div--item')
        if texts==[] :
            return
        texts2=BeautifulSoup(str(texts[0]))
        b_1=texts2.find_all('ul',class_='item-craft-list')
        b_2=BeautifulSoup(str(b_1[0]),parse_only=None)
        tag=b_2.li.ul
        for temp in tag.previous_sibling.stripped_strings:
            if '×' in temp:
                outNum=int(temp[1:])
            else: outNum=1
        itemDict['产量']=[str(outNum),usedList]
        tempList=list()
        for child in tag.children:
            for string in child.div.stripped_strings:
                if '[' in string or '/' in string or ']' in string:
                    pass
                else: tempList.append(string)
            tempList[1]=tempList[1][1:]
            itemDict[tempList[0]]=list(tempList[1:])
            tempList.clear()
        print(itemDict)
        return

    def work(self,item,used=False):
        item=item.strip()
        itemDict={}
        self.__searchItem(item,itemDict,used)
        print("\n\n所查物品为：",item)
        print("\n查得材料列表为：")
        list1=list(itemDict.keys())[1:]
        for name in list1:
            list1[list1.index(name)]=list1[list1.index(name)]+' ×'+itemDict[name][0]
        print(list1)
        return itemDict




if __name__ == '__main__':
    a=firstSearch()
    item="灵银制敌头盔"
    result=a.work(item,True)
    